﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using UBCon.Resources;
using UBCon.Ribbon.Interfaces;

namespace UBCon.Ribbon
{
    internal class ContextMenuHelper
    {
        private static ContextMenu contextMenu;

        internal static ContextMenu ContextMenu 
        {
            get 
            {
                if (contextMenu == null) 
                {
                    contextMenu = BuildContextMenu();
                }
                return contextMenu;
            }
        }

        private static ContextMenu BuildContextMenu() 
        {
            ContextMenu context = new ContextMenu();
            Style style = RibbonHelper.GetResourceByKey<Style>("DefaultContextMenuStyle");
            if (style != null) 
            {
                context.Style = style;
            }
            context.Items.Add(new DropDownMenuItem() { Header = LocalizationResources.AddToQAToolbarText });
            context.Items.Add(new DropDownMenuItem() { Header = LocalizationResources.QAToolbarSettingsText });
            context.Items.Add(new DropDownMenuItem() { Header = LocalizationResources.PlaceQAunderRibbonBarText });
            context.Items.Add(new DropDownMenuItem() { Header = LocalizationResources.MinimizeRibbonBarText });
            return context;
        }

        internal static IRibbonControl GetHitTestElement(FrameworkElement elt) 
        {
            IRibbonControl control = null;

            FrameworkElement element = elt.TemplatedParent as FrameworkElement;
            
            if (element != null && element is IRibbonControl&&!(element is RibbonChrome))
            {
                control = element as IRibbonControl;
                return control;
            }
            
            while (element != null)
            {
                element = element.TemplatedParent as FrameworkElement;
                if (element != null && element is IRibbonControl && !(element is RibbonChrome))
                {
                    control = element as IRibbonControl;
                }
            }
            
            return control;
        }
    }
}
